cc.Class({
    extends: cc.Component,

    properties: {
        coinNum: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.bool = true;
        this.Arr = [];
    },
    coinAddEffect(num1, num2) {
        var obj = {
            num1: num1,
            num2: num2,
        };
        this.Arr.push(obj);
    },
    /**
     *      文字增加滚动
     * @param target 文字的节点
     * @param options 是一个对象 里内容有time: 特效时间, initNum: 初始金币, num: 达到的金币数, regulator: 调速器
     */
    playEffect(num1, num2) {
        var that = this;
        // options = options || {};
        var options = {
            time: 1500,
            initNum: num1,
            num: num2,
            regulator: 50
        };

        if (options.initNum == options.num) return;
        var time = options.time,//总时间--毫秒为单位
            finalNum = options.num, //要显示的真实数值
            regulator = options.regulator || 100, //调速器，改变regulator的数值可以调节数字改变的速度
            step = (finalNum - options.initNum) / (time / regulator), /*每30ms增加的数值--*/
            count = options.initNum, //计数器
            initial = options.initNum;
        var timer = setInterval(() => {
            count = count + step;
            if (count >= finalNum && options.initNum < finalNum) {
                that.bool = true;
                clearInterval(timer);
                count = finalNum;
            }
            if (count <= finalNum && options.initNum > finalNum) {
                clearInterval(timer);
                that.bool = true;
                count = finalNum;
            }
            //t未发生改变的话就直接返回
            var t = Math.floor(count);
            if (t == initial) return;
            initial = t;
            if (this.coinNum) {
                this.coinNum.string = initial + "";
            }
        }, 30);
    },
    start() {

    },
    update(dt) {
        if (this.bool) {
            if (this.Arr[0]) {
                this.bool = false;
                this.playEffect(this.Arr[0].num1, this.Arr[this.Arr.length - 1].num2);
                this.Arr = [];
            }
        }
    },
});
